import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { Language } from "../../../../extend/language/Language";
import { GLoadedButton } from "../../../../com/mui/controls/GLoadedButton";
import { EffectManager } from "../../manager/EffectManager";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { CursorManager } from "../../manager/CursorManager";
import { PetIslandUtil } from "./PetIslandUtil";
import { SPlaceMiniInfo } from "../../../../Message/Game/SPlaceMiniInfo";
import { PetIslandMapItemTip } from "./PetIslandMapItemTip";
import { Cache } from "../../cache/Cache";
import { ObjCreate } from "../common/ObjCreate";
import { SIslandPos } from "../../../../Message/Game/SIslandPos";
import { PetIslandMapItem } from "./renderer/PetIslandMapItem";
import { PetIslandRulePanel } from "./PetIslandRulePanel";
import { ImagesConst } from "../../resource/ImagesConst";
import { PetIslandMapItemButton } from "./PetIslandMapItemButton";
import { ResFileConst } from "../../resource/ResFileConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { DisplayUtil } from "../../../common/DisplayUtil";
import { SystemSetter } from "../system/SystemSetter";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GameProxy } from "../../mvc/GameProxy";
import { EIsland } from "../../../../Message/Game/EIsland";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class PetIslandMapPanel
    
    export  class PetIslandMapPanel extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            this._blueMapItems = new Map<any, any> /* flash.utils.Dictionary */();
            this._purpleMapItems = new Map<any, any> /* flash.utils.Dictionary */();
            this._orangeMapItems = new Map<any, any> /* flash.utils.Dictionary */();
            this._maskWindowSize = {"w": 800, "h": 499};
            this._maskWindowPoint = {"x": 18, "y": 38};
            super(arg1);
            setSize(836, 550);
            titleHeight = 26;
            title = Language.getString(28100);
            return;
        }

        public infoChange(): void
        {
            var loc1=this._uiSp.getChildByName("fightBtn") as GLoadedButton;
            if (loc1) 
            {
                EffectManager.glowFilterUnReg(loc1);
                EffectManager.glowFilterReg(loc1, [FilterConst.guidePetCall], 1, 10, 0, 10);
            }
            return;
        }

        /* internal  */onSmallMapMouseDownHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._smallMap.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onSmallMapMouseMoveHandler);
            this._smallMap.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onSmallMapMouseUpHandler);
            CursorManager.showCursor(CursorManager.Fist);
            this._smallMapItemUI.startDrag();
            return;
        }

        /* internal  */onSmallMapMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._smallMap.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onSmallMapMouseMoveHandler);
            this._smallMap.removeEventListener(flash.events.MouseEvent.ROLL_OUT, this.onSmallMapMouseUpHandler);
            this._smallMapItemUI.stopDrag();
            this.onSmallMapMouseMoveHandler();
            if (arg1.type != flash.events.MouseEvent.MOUSE_UP) 
            {
                if (arg1.type == flash.events.MouseEvent.ROLL_OUT) 
                {
                    CursorManager.hideCursor();
                }
            }
            else 
            {
                CursorManager.showCursor(CursorManager.Palm);
            }
            return;
        }

        /* internal  */onSmallMapMouseOverHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            CursorManager.showCursor(CursorManager.Palm);
            return;
        }

        /* internal  */onSmallMapMouseOutHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            CursorManager.hideCursor();
            return;
        }

        /* internal  */onSmallMapMouseMoveHandler(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            if (this._smallMapItemUI.y > this._maskWindowPoint.y) 
            {
                this._smallMapItemUI.y = this._maskWindowPoint.y;
            }
            if (this._smallMapItemUI.y < this._maskWindowSize.h - PetIslandUtil.mapSizeDic[this._type] + this._maskWindowPoint.y) 
            {
                this._smallMapItemUI.y = this._maskWindowSize.h - PetIslandUtil.mapSizeDic[this._type] + this._maskWindowPoint.y;
            }
            if (this._smallMapItemUI.x > this._maskWindowPoint.x) 
            {
                this._smallMapItemUI.x = this._maskWindowPoint.x;
            }
            if (this._smallMapItemUI.x < this._maskWindowSize.w - PetIslandUtil.mapSizeDic[this._type] + this._maskWindowPoint.x) 
            {
                this._smallMapItemUI.x = this._maskWindowSize.w - PetIslandUtil.mapSizeDic[this._type] + this._maskWindowPoint.x;
            }
            return;
        }

        public updateItem(arg1: SPlaceMiniInfo): void
        {
            var loc1=this.getItemByPos(arg1.pos);
            if (loc1) 
            {
                loc1.data = arg1;
            }
            return;
        }

        public set mapItemTipPosition(arg1: PetIslandMapItemTip)
        {
            var loc1=this.getItemByPos(Cache.instance.petIsland.curPos.pos);
            if (loc1) 
            {
                if (loc1.x + PetIslandUtil.MAP_SIZE + 230 > PetIslandUtil.mapSizeDic[Cache.instance.petIsland.curPos.pos.isLand]) 
                {
                    ObjCreate.setObjAttri(arg1, -230, 20, -1, -1, loc1);
                }
                else 
                {
                    ObjCreate.setObjAttri(arg1, 160, 20, -1, -1, loc1);
                }
                loc1.parent.addChild(loc1);
            }
            Cache.instance.petIsland.curPos.pos = null;
            return;
        }

        /* internal  */getItemByPos(arg1: SIslandPos): PetIslandMapItem
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (arg1 && arg1.isLand == this._type) 
            {
                loc2 = this.getDicByType(arg1.isLand);
                loc3 = arg1.x.toString() + arg1.y;
                loc1 = loc2[loc3] as PetIslandMapItem;
            }
            return loc1;
        }

        /* internal  */get rulePanel(): PetIslandRulePanel
        {
            if (!this._rulePanel) 
            {
                this._rulePanel = new PetIslandRulePanel();
                this._rulePanel.x = this.width + 10;
                this._rulePanel.y = 100;
            }
            return this._rulePanel;
        }

        /* internal  */createBigMapUI(): void
        {
            this._bg = ObjCreate.createGImageBitmap(ImagesConst.PetIslandMapBigMapBg, 18, 38, this._bigMap);
            var loc1=new PetIslandMapItemButton(545, 145);
            loc1.styleName = ResFileConst.PetIslandMapBlueMapBtn;
            loc1.label = "";
            loc1.name = "blueMapBtn";
            ObjCreate.setObjAttri(loc1, 100, 101, 545, 145, this._bigMap);
            var loc2=new PetIslandMapItemButton(338, 217);
            loc2.styleName = ResFileConst.PetIslandMapPurpleMapBtn;
            loc2.label = "";
            loc2.name = "purpleMapBtn";
            ObjCreate.setObjAttri(loc2, 483, 235, 338, 217, this._bigMap);
            var loc3=new PetIslandMapItemButton(327, 203);
            loc3.styleName = ResFileConst.PetIslandMapOrangeMapBtn;
            loc3.label = "";
            loc3.name = "orangeMapBtn";
            ObjCreate.setObjAttri(loc3, 18, 265, 327, 203, this._bigMap);
            return;
        }

        /* internal  */createSmallMapUI(): void
        {
            this._smallMapBg = ObjCreate.createGImageBitmap("");
            return;
        }

        /* internal  */onResCompl(): void
        {
            ObjCreate.createGBitmap(ImagesConst.PetIslandMapTopText, 277, 56, this._bigMap);
            ObjCreate.createGBitmap(ImagesConst.PetIslandMapLeftText, 42, 56, this._bigMap);
            ObjCreate.createBg(0, 0, 148, 32, this._bigMap, ImagesConst.SelectedBg);
            var loc1=this._uiSp.getChildIndex(this._mapFullBtn);
            var loc2=ObjCreate.createGBitmap(ImagesConst.PetIslandMapSmallMapBtnBg, 629, 456);
            this._uiSp.addChildAt(loc2, loc1);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this._uiSp = new Object /* flash.display.Sprite */();
            this._uiSp.mouseEnabled = false;
            this._uiSp.addEventListener(flash.events.MouseEvent.CLICK, this.onClickBigMapHandler);
            this.addChild(this._uiSp);
            this._bigMap = new Object /* flash.display.Sprite */();
            this._bigMap.mouseEnabled = false;
            ObjCreate.setObjAttri(this._bigMap, 0, 0, -1, -1);
            this._smallMapMark = new Object /* flash.display.Sprite */();
            this._smallMapMark.graphics.beginFill(15859633, 1);
            this._smallMapMark.graphics.drawRect(0, 0, 800, 499);
            this._smallMapMark.graphics.endFill();
            this._smallMapMark.mouseEnabled = false;
            ObjCreate.setObjAttri(this._smallMapMark, 18, 38, 800, 499, this);
            this._smallMap = new Object /* flash.display.Sprite */();
            this._smallMap.mouseEnabled = false;
            this._smallMap.buttonMode = true;
            this._smallMap.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onSmallMapMouseDownHandler);
            this._smallMap.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onSmallMapMouseUpHandler);
            this._smallMap.addEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onSmallMapMouseOverHandler);
            this._smallMap.addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onSmallMapMouseOutHandler);
            ObjCreate.setObjAttri(this._smallMap, 0, 0, -1, -1);
            this._smallMapItemUI = new Object /* flash.display.Sprite */();
            this._smallMapItemUI.mask = this._smallMapMark;
            ObjCreate.setObjAttri(this._smallMapItemUI, 0, 0, -1, -1, this._smallMap);
            this._mapFullBtn = ObjCreate.createGLoadedButton(ResFileConst.PetIslandMapFullBtn, 734, 478, 55, 63, this._uiSp);
            this._mapFullBtn.name = "mapFullBtn";
            var loc1=ObjCreate.createGLoadedButton(ResFileConst.PetIslandMapPowerBtn, 744, 442, 39, 43, this._uiSp);
            loc1.name = "powerBtn";
            var loc2=ObjCreate.createGLoadedButton(ResFileConst.PetIslandMapRuleBtn, 781, 460, 39, 43, this._uiSp);
            loc2.name = "ruleBtn";
            this._curMapName = ObjCreate.createGTextField("", 25, 45, 80, 40, this._uiSp, new GTextFormat(FontUtil.xingkaiName, 18, 11661311, null, null, null, null, null, flash.text.TextFormatAlign.LEFT));
            this.createBigMapUI();
            this.createSmallMapUI();
            this.createMapItems();
            LoaderHelp.addResCallBack(ResFileConst.PetIslandMap, this.onResCompl);
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this._curMapName.text = "";
            DisplayUtil.removeMe(this._smallMap);
            this._uiSp.addChildAt(this._bigMap, 0);
            if (Cache.instance.clientSettingCache.isFirstOpenPetIslandView) 
            {
                Cache.instance.clientSettingCache.isFirstOpenPetIslandView = false;
                SystemSetter.systemGuideSetterInfo.setSystemInfo("isFirstOpenPetIslandView", false);
                Dispatcher.dispatchEvent(new DataEvent(EventName.SaveGuideSystemSetter));
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandPicRulePanelShow));
                this.addChild(this.rulePanel);
            }
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            DisplayUtil.removeMe(this._smallMap);
            DisplayUtil.removeMe(this._rulePanel);
            return;
        }

        /* internal  */onClickBigMapHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc3=0;
            var loc1=arg1.target as GLoadedButton;
            if (loc1) 
            {
                var loc4=loc1.name;
                switch (loc4) 
                {
                    case "mapFullBtn":
                    {
                        this._curMapName.text = "";
                        DisplayUtil.removeMe(this._smallMap);
                        this._uiSp.addChildAt(this._bigMap, 0);
                        return;
                    }
                    case "powerBtn":
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandPowerBaseViewShowByIndex, 0));
                        return;
                    }
                    case "fightBtn":
                    {
                        GameProxy.petIsland.getGuildInfo();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandPowerBaseViewShowByIndex, 2));
                        return;
                    }
                    case "ruleBtn":
                    {
                        if (this.rulePanel.parent) 
                        {
                            DisplayUtil.removeMe(this.rulePanel);
                        }
                        else 
                        {
                            this.addChild(this.rulePanel);
                        }
                        return;
                    }
                    case "blueMapBtn":
                    {
                        this._type = EIsland._EIslandBlue;
                        this.middleSmallMapItemUI();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandMapGetIslandInfo, this._type));
                        break;
                    }
                    case "purpleMapBtn":
                    {
                        this._type = EIsland._EIslandPurple;
                        this.middleSmallMapItemUI();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandMapGetIslandInfo, this._type));
                        break;
                    }
                    case "orangeMapBtn":
                    {
                        this._type = EIsland._EIslandOrange;
                        this.middleSmallMapItemUI();
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandMapGetIslandInfo, this._type));
                        break;
                    }
                }
            }
            return;
        }

        /* internal  */createMapItems(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            loc1 = 0;
            while (loc1 < 20) 
            {
                loc2 = 0;
                while (loc2 < 20) 
                {
                    loc3 = loc1.toString() + loc2.toString();
                    if (loc1 < 8 && loc2 < 8) 
                    {
                        this._blueMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandBlue);
                        this._purpleMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandPurple);
                        this._orangeMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandOrange);
                    }
                    else if (loc1 < 12 && loc2 < 12) 
                    {
                        this._purpleMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandPurple);
                        this._blueMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandBlue);
                    }
                    else if (loc1 < 20 && loc2 < 20) 
                    {
                        this._blueMapItems[loc3] = new PetIslandMapItem(EIsland._EIslandBlue);
                    }
                    ++loc2;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */middleSmallMapItemUI(): void
        {
            var loc1=0;
            loc1 = PetIslandUtil.mapSizeDic[this._type];
            this._smallMapItemUI.x = (this._maskWindowSize.w - loc1) / 2 + this._maskWindowPoint.x;
            this._smallMapItemUI.y = (this._maskWindowSize.h - loc1) / 2 + this._maskWindowPoint.y;
            this._smallMapBg.bitmapData = null;
            return;
        }

        /* internal  */set middleSmallMapPoint(arg1: SIslandPos)
        {
            var loc4=0;
            var loc5=0;
            var loc1=PetIslandUtil.mapSizeDic[this._type];
            var loc2=new Object /* flash.geom.Point */(loc1 / 2, loc1 / 2);
            var loc3=new Object /* flash.geom.Point */(this._smallMapItemUI.x, this._smallMapItemUI.y);
            var loc6=Math.abs(loc2.x - arg1.x * PetIslandUtil.MAP_SIZE);
            var loc7=Math.abs(loc2.y - arg1.y * PetIslandUtil.MAP_SIZE);
            if (arg1.x * PetIslandUtil.MAP_SIZE <= loc2.x && arg1.y * PetIslandUtil.MAP_SIZE <= loc2.y) 
            {
                loc6 = loc6 - PetIslandUtil.MAP_SIZE / 2;
                loc7 = loc7 - PetIslandUtil.MAP_SIZE / 2;
                loc3.x = loc3.x + loc6;
                loc3.y = loc3.y + loc7;
                loc4 = this._maskWindowPoint.x;
                loc5 = this._maskWindowPoint.y;
                this._smallMapItemUI.x = loc3.x > loc4 ? loc4 : loc3.x;
                this._smallMapItemUI.y = loc3.y > loc5 ? loc5 : loc3.y;
            }
            else if (arg1.x * PetIslandUtil.MAP_SIZE >= loc2.x && arg1.y * PetIslandUtil.MAP_SIZE <= loc2.y) 
            {
                loc6 = loc6 + PetIslandUtil.MAP_SIZE / 2;
                loc7 = loc7 - PetIslandUtil.MAP_SIZE / 2;
                loc3.x = loc3.x - loc6;
                loc3.y = loc3.y + loc7;
                loc4 = this._maskWindowPoint.x + this._maskWindowSize.w - loc1;
                loc5 = this._maskWindowPoint.y;
                this._smallMapItemUI.x = loc3.x < loc4 ? loc4 : loc3.x;
                this._smallMapItemUI.y = loc3.y > loc5 ? loc5 : loc3.y;
            }
            else if (arg1.x * PetIslandUtil.MAP_SIZE <= loc2.x && arg1.y * PetIslandUtil.MAP_SIZE >= loc2.y) 
            {
                loc6 = loc6 - PetIslandUtil.MAP_SIZE / 2;
                loc7 = loc7 + PetIslandUtil.MAP_SIZE / 2;
                loc3.x = loc3.x + loc6;
                loc3.y = loc3.y - loc7;
                loc4 = this._maskWindowPoint.x;
                loc5 = this._maskWindowPoint.y + this._maskWindowSize.h - loc1;
                this._smallMapItemUI.x = loc3.x > loc4 ? loc4 : loc3.x;
                this._smallMapItemUI.y = loc3.y < loc5 ? loc5 : loc3.y;
            }
            else if (arg1.x * PetIslandUtil.MAP_SIZE >= loc2.x && arg1.y * PetIslandUtil.MAP_SIZE >= loc2.y) 
            {
                loc6 = loc6 + PetIslandUtil.MAP_SIZE / 2;
                loc7 = loc7 + PetIslandUtil.MAP_SIZE / 2;
                loc3.x = loc3.x - loc6;
                loc3.y = loc3.y - loc7;
                loc4 = this._maskWindowPoint.x + this._maskWindowSize.w - loc1;
                loc5 = this._maskWindowPoint.y + this._maskWindowSize.h - loc1;
                this._smallMapItemUI.x = loc3.x < loc4 ? loc4 : loc3.x;
                this._smallMapItemUI.y = loc3.y < loc5 ? loc5 : loc3.y;
            }
            return;
        }

        /* internal  */getDicByType(arg1: int): Map<any, any> /* flash.utils.Dictionary */
        {
            if (arg1 == EIsland._EIslandBlue) 
            {
                return this._blueMapItems;
            }
            if (arg1 == EIsland._EIslandPurple) 
            {
                return this._purpleMapItems;
            }
            return this._orangeMapItems;
        }

        public setSmallMapData(arg1: Array<any>, arg2: Boolean, arg3: SIslandPos): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            if (arg2) 
            {
                this._type = arg3.isLand;
                this.middleSmallMapItemUI();
                this.middleSmallMapPoint = arg3;
            }
            if (arg1.length > 0 && !(SPlaceMiniInfo(arg1[0]).pos.isLand == this._type)) 
            {
                return;
            }
            this._curMapName.htmlText = GameDefConfig.instance.getPetIslandName(this._type);
            DisplayUtil.removeMe(this._bigMap);
            this._uiSp.addChildAt(this._smallMap, 0);
            DisplayUtil.removeAllChild(this._smallMapItemUI);
            var loc1=this.getDicByType(this._type);
            this._smallMapBg.imgUrl = "PetIslandMapSmallMap_" + this._type;
            ObjCreate.setObjAttri(this._smallMapBg, this._smallMapBg.x, this._smallMapBg.y, -1, -1, this._smallMapItemUI);
            var loc5=0;
            var loc6=loc1;
            for (loc3 in loc6) 
            {
                (loc2 = loc1[loc3] as PetIslandMapItem).clean();
            }
            loc5 = 0;
            loc6 = arg1;
            for(loc4 of loc6) 
            {
                loc3 = loc4.pos.x.toString() + loc4.pos.y;
                (loc2 = loc1[loc3] as PetIslandMapItem).data = loc4;
                if (arg2 && loc4.pos.x == arg3.x && loc4.pos.y == arg3.y) 
                {
                    loc2.setFilter();
                }
                ObjCreate.setObjAttri(loc2, loc4.pos.x * PetIslandUtil.MAP_SIZE, loc4.pos.y * PetIslandUtil.MAP_SIZE, -1, -1, this._smallMapItemUI);
            }
            return;
        }

        /* internal */ /* var */_bg: GImageBitmap;

        /* internal */ /* var */_uiSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bigMap: Object /* flash.display.Sprite */;

        /* internal */ /* var */_smallMap: Object /* flash.display.Sprite */;

        /* internal */ /* var */_smallMapItemUI: Object /* flash.display.Sprite */;

        /* internal */ /* var */_smallMapMark: Object /* flash.display.Sprite */;

        /* internal */ /* var */_smallMapBg: GImageBitmap;

        /* internal */ /* var */_type: int;

        /* internal */ /* var */_blueMapItems: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_purpleMapItems: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_orangeMapItems: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_maskWindowSize: Object;

        /* internal */ /* var */_maskWindowPoint: Object;

        /* internal */ /* var */_rulePanel: PetIslandRulePanel;

        /* internal */ /* var */_curMapName: GTextFiled;

        /* internal */ /* var */_mapFullBtn: GLoadedButton;
    }
